package com.zzh.bc.dao.api.impl;

import com.zzh.bc.dao.api.ApiParamDao;
import com.zzh.bc.db.C3p0Utils;
import com.zzh.bc.db.MySQLExecute;
import com.zzh.bc.model.ApiModel;
import com.zzh.bc.model.ApiParamModel;
import com.zzh.bc.model.PageModel;
import com.zzh.bc.modules.api.vo.ApiParamVo;

import java.sql.SQLException;
import java.util.List;

/**
 * 参数Dao
 */
public class ApiParamDaoImpl implements ApiParamDao {
    @Override
    public ApiParamVo findObjById(String id) {
        return null;
    }

    @Override
    public List<ApiParamVo> findAllObjByPage(PageModel pageModel) {
        return null;
    }

    @Override
    public void beginTransaction() {
        try {
            C3p0Utils.beginTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean save(ApiParamModel model) {
        String sql = "INSERT INTO  " + ApiParamModel.TABLE_NAME + " (" + ApiParamModel.SQL_COLUMN_NAME + ") VALUES (" + ApiParamModel.SQL_COLUMN_PARAMS + ")";
        try {
            boolean flagSaveApiModel = MySQLExecute.insert(sql,
                    model.getApiParamId(),
                    model.getApiParamName(),
                    model.getApiReqMethodId(),
                    model.getApiParamKey(),
                    model.getApiParamValue(),
                    model.getApiProjectId(),
                    model.getApiParamsDetails(),
                    model.getCreatedBy(),
                    model.getCreatedTime(),
                    model.getUpdatedBy(),
                    model.getUpdatedTime()) == 1;
            return flagSaveApiModel;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(ApiParamModel sub) {
        String sql = ApiParamModel.SQL_UPDATE_COLUMN_NAME;
        try {
            boolean flagSaveApiModel = MySQLExecute.insert(sql,
                    sub.getApiParamName(),
                    sub.getApiReqMethodId(),
                    sub.getApiParamKey(),
                    sub.getApiParamValue(),
                    sub.getApiProjectId(),
                    sub.getApiParamsDetails(),
                    sub.getCreatedBy(),
                    sub.getCreatedTime(),
                    sub.getUpdatedBy(),
                    sub.getUpdatedTime(), sub.getApiParamId()) == 1;
            return flagSaveApiModel;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(String apiId) {
        String sql = ApiParamModel.SQL_DELETE;
        try {
            boolean flagSaveApiModel = MySQLExecute.delete(sql, apiId) >= 0;
            return flagSaveApiModel;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
